package com.ruoyi.system.vo.weishuapay;

import java.util.UUID;
import java.util.logging.Logger;

import com.ruoyi.common.utils.PropertiesConst;
import com.ruoyi.system.vo.CommonUtil;
import com.ruoyi.system.vo.TransUtil;


public class KuaijieUtil {

	private static Logger logger4J = Logger.getLogger(KuaijieUtil.class
			.getSimpleName());

	public static String kuaijie(String amount,String bankcard,String accountName,
			String mobile
			,String cvn2,
			String expireDate,String subMerchantNo,
			String province,String returnUrl,String notifyUrl,String disorderid) {
		KuaijieBean trans = new KuaijieBean();
		trans.setTranscode(PropertiesConst.WeishuaPay_REGISTER_TRANSCODE_041);
		trans.setVersion(PropertiesConst.WeishuaPay_Version);
		trans.setOrdersn(UUID.randomUUID().toString().replaceAll("-", ""));
		trans.setMerchno(PropertiesConst.WeishuaPay_MERCHNO);
		trans.setDsorderid(disorderid);
		trans.setAmount(amount);
		trans.setBankcard(bankcard);
		trans.setAccountName(accountName);
		trans.setMobile(mobile);
		trans.setCvn2(cvn2);
		trans.setExpireDate(expireDate);
		trans.setSubMerchantNo(subMerchantNo);
		trans.setProvince(province);
		trans.setReturnUrl(returnUrl);
		trans.setNotifyUrl(notifyUrl);
		return send(trans);
	}
	
	public static String geturl(String json) {
		TransUtil tu = new TransUtil();
		KuaijieReturnBean json2Kuaijie = tu.json2Kuaijie(json);
		String pay_code = json2Kuaijie.getPay_code();
		PayCodeBean payCode = tu.json2PayCode(pay_code);
		String parm = "?";
		parm+="orderid="+payCode.getOrderid()+"&";
		parm+="merchno="+payCode.getMerchno()+"&";
		parm+="bankcard="+payCode.getBankcard()+"&";
		parm+="accountName="+payCode.getAccountName()+"&";
		parm+="mobile="+payCode.getMobile()+"&";
		parm+="returnUrl="+payCode.getReturnUrl()+"&";
		parm+="notifyUrl="+payCode.getNotifyUrl()+"&";
		parm+="sign="+payCode.getSign()+"&";
		parm+="bankcode="+payCode.getBankcard();
		return json2Kuaijie.getPay_info()+parm;
	}
	
	public static String send(KuaijieBean trans) {
		try {
			TransUtil tu = new TransUtil();
			byte[] reponse = tu.packet(trans, PropertiesConst.WeishuaPay_MERCHKEY);
			String response = CommonUtil.post(PropertiesConst.WeishuaPayURL_KUAIJIE_PAY, reponse);
			logger4J.info("response:"+response);
			return response;
		} catch (Exception e) {
			logger4J.info("err:"+e.getLocalizedMessage());
		}
		return null;
	}
	
	
	/**
	 * 检查交易是否返回成功
	 * @param response
	 * @return
	 */
	public static boolean isJobSuccess(String response) {
		KuaijieReturnBean bean = jason2bean(response);
		if("0000".equals(bean.getReturncode())){
			return true;
		}
		return false;
	}
	
	/**
	 * Jason转换为bean
	 * @param response
	 * @return
	 */
	public static KuaijieReturnBean jason2bean(String response) {
		TransUtil tu = new TransUtil();
		KuaijieReturnBean bean = tu.json2Kuaijie(response);
		return bean;
	}
	
}
